package me.hys.service.doctor.impl;

import java.sql.Timestamp;
import java.util.List;

import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.bo.company.MessageBo;
import me.hys.bo.company.ProductBo;
import me.hys.bo.doctor.DoctorBo;
import me.hys.cons.ApiStatus;
import me.hys.dao.doctor.ICollectionMapper;
import me.hys.entity.doctor.CollectionEntity;
import me.hys.kits.HtmlKit;
import me.hys.service.doctor.ICollectionManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author: @author HYS . <br>
 * Date: 2015-10-11 下午8:33:08 . <br>
 * Function:
 */
@Service
@Transactional
class CollectionManagerImpl implements ICollectionManager {
	@Autowired
	private ICollectionMapper collectionMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.doctor.ICollectionManager#delete(int)
	 */
	@Override
	public ApiResult delete(int id) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS, "操作成功");
		if (collectionMapper.delete(id) == 0) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg("记录不存在");
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICollectionManager#deleteByUnionKey(me.hys.entity
	 * .doctor.CollectionEntity)
	 */
	@Override
	public ApiResult deleteByUnionKey(CollectionEntity entity) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS, "操作成功");
		if (collectionMapper.deleteByUnionKey(entity) == 0) {
			apiResult.setResult(ApiStatus.FAILED);
			apiResult.setMsg("记录不存在");
		}
		return apiResult;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICollectionManager#findDoctorPage(me.hys.base.PageQuery
	 * )
	 */
	@Override
	public PageQuery<DoctorBo> findDoctorPage(PageQuery<DoctorBo> page) {
		page.setList(collectionMapper.findDoctorPage(page.getCondition()));
		page.setTotal(collectionMapper.findDoctorPageCount(page.getCondition()));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICollectionManager#findMessagePage(me.hys.base.
	 * PageQuery)
	 */
	@Override
	public PageQuery<MessageBo> findMessagePage(PageQuery<MessageBo> page) {
		List<MessageBo> list = collectionMapper.findMessagePage(page
				.getCondition());
		for (MessageBo entity : list) {
			entity.setContent(HtmlKit.filterSubstring(entity.getContent(),
					HtmlKit.LENGTH));
		}
		page.setList(list);
		page.setTotal(collectionMapper.findMessagePageCount(page.getCondition()));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * me.hys.service.doctor.ICollectionManager#findProductPage(me.hys.base.
	 * PageQuery)
	 */
	@Override
	public PageQuery<ProductBo> findProductPage(PageQuery<ProductBo> page) {
		page.setList(collectionMapper.findProductPage(page.getCondition()));
		page.setTotal(collectionMapper.findProductPageCount(page.getCondition()));
		// 总页数
		int size = (int) Math.ceil(page.getTotal() * 1.0f / page.getLimit());
		page.setSize(size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.doctor.ICollectionManager#save(me.hys.entity.doctor.
	 * CollectionEntity)
	 */
	@Override
	public ApiResult save(CollectionEntity entity) {
		ApiResult apiResult = new ApiResult(ApiStatus.SUCCESS, "操作成功");
		if (collectionMapper.findByUnionKey(entity) == 0) {
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			collectionMapper.save(entity);
		}
		return apiResult;
	}
}
